package com.supreme.uploadserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.uploadserver.service.FileProcService;
import com.supreme.uploadserver.service.MinioService;
import io.minio.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "minio")
public class MinioController {

    @Value("${upload-server.uploadPath}")
    private String supremeUploadPath;

    @Resource
    private FileProcService fileProcService;

    @Resource
    private MinioClient minioClient;

    @Resource
    private MinioService minioService;

    @GetMapping(value = "bucketExists/{bucketName}")
    @SneakyThrows(Exception.class)
    public AjaxResp bucketExists(@PathVariable(value = "bucketName") String bucketName) {
        BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(bucketName).build();
        boolean bucketExists = minioClient.bucketExists(bucketExistsArgs);
        return AjaxResp.instance(bucketExists);
    }

    @GetMapping(value = "getBucketList")
    @SneakyThrows(Exception.class)
    public AjaxResp getBucketList() {
        List<Bucket> bucketList = minioClient.listBuckets();
        List<String> bucketNameList = bucketList.stream().map(Bucket::name).collect(Collectors.toList());
        return AjaxResp.instance(bucketNameList);
    }

    @PostMapping(value = "createBucket/{bucketName}")
    @SneakyThrows(Exception.class)
    public AjaxResp createBucket(@PathVariable(value = "bucketName") String bucketName) {
        BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(bucketName).build();
        boolean isExist = minioClient.bucketExists(bucketExistsArgs);
        if (!isExist) {
            MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder()
                    .bucket(bucketName)
                    .build();
            minioClient.makeBucket(makeBucketArgs);
        }
        return AjaxResp.instance();
    }

    @DeleteMapping(value = "removeBucket/{bucketName}")
    @SneakyThrows(Exception.class)
    public AjaxResp removeBucket(@PathVariable(value = "bucketName") String bucketName) {
        BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(bucketName).build();
        boolean isExist = minioClient.bucketExists(bucketExistsArgs);
        if (isExist) {
            final RemoveBucketArgs removeBucketArgs = RemoveBucketArgs.builder()
                    .bucket(bucketName)
                    .build();
            minioClient.removeBucket(removeBucketArgs);
        }
        return AjaxResp.instance();
    }

    @GetMapping(value = "getObjectList/{bucketName}")
    @SneakyThrows(Exception.class)
    public AjaxResp getObjectList(@PathVariable(value = "bucketName") String bucketName) {
        final ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder()
                .bucket(bucketName)
                .build();
        final Iterable<Result<Item>> objectList = minioClient.listObjects(listObjectsArgs);

        List<String> fileList = new ArrayList<>();
        for (Result<Item> itemResult : objectList) {
            final Item item = itemResult.get();
            fileList.add(item.objectName());
        }

        return AjaxResp.instance(fileList);
    }
}
